package models

import (
	"go-admin-official/modules/database"
)

type PluginsCommentsModel struct {
	Base `json:"-"`

	Id        int64  `json:"id"`
	PluginId  int64  `json:"plugin_id"`
	UserId    int64  `json:"user_id"`
	Content   string `json:"content"`
	CreatedAt string `json:"created_at"`
}

func PluginsComments() PluginsCommentsModel {
	return PluginsCommentsModel{Base: Base{Table: "plugins_comments"}}
}

func (t PluginsCommentsModel) WithTx(tx *database.SqlTx) PluginsCommentsModel {
	t.Tx = tx
	return t
}

func (t PluginsCommentsModel) Find(id interface{}) PluginsCommentsModel {
	item, _ := database.Table(t.Table).Find(id)

	if item == nil {
		return PluginsComments()
	}

	return t.MapToModel(item)
}

func (t PluginsCommentsModel) Update(key string, value interface{}) (int64, error) {
	return database.Table(t.Table).
		Where("id", "=", t.Id).
		Update(database.H{
			key: value,
		})
}

func (t PluginsCommentsModel) IsEmpty() bool {
	return t.Id == int64(0)
}

func (t PluginsCommentsModel) IsNotEmpty() bool {
	return t.Id != int64(0)
}

func (t PluginsCommentsModel) MapToModel(m map[string]interface{}) PluginsCommentsModel {
	t.Id, _ = m["id"].(int64)
	t.PluginId, _ = m["plugin_id"].(int64)
	t.UserId, _ = m["user_id"].(int64)
	t.Content, _ = m["content"].(string)
	t.CreatedAt, _ = m["created_at"].(string)
	return t
}
